﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private List<List<List<string>>> _maps, _logs;
        private List<Int32> _frame_nr;
        private Int32 _map_number;
        public Form1()
        {
            InitializeComponent();
            _maps = new List<List<List<string>>>();
            _logs = new List<List<List<string>>>();
            _frame_nr = new List<Int32>();
            _map_number = 0;
            this.Width = 1100;
            this.Height = 600;
            listBox1.Width = 500;
            listBox1.Height = 500;
            listBox2.Width = 800;
            listBox2.Height = 500;
            numericUpDown1.Enabled = false;
            numericUpDown2.Enabled = false;
            checkBox1.Enabled = false;
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            _maps = new List<List<List<string>>>();
            _logs = new List<List<List<string>>>();
            _frame_nr = new List<Int32>();
            _map_number = 0;
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            if (openFileDialog1.ShowDialog() == DialogResult.OK) // ha az ablakot Ok-val zártuk be
            {
                try
                {
                    this.Text = "Strategy map -- " + openFileDialog1.FileName;
                    string[] lines = System.IO.File.ReadAllLines(openFileDialog1.FileName);
                    //string[] lines = System.IO.File.ReadAllLines("3.txt");
                    Int32 i = 0;
                    while (i < lines.Length)
                    {
                        //uj map kezdődik
                        if (lines[i] == "Tick: 0")
                        {
                            ++i;
                            while (i < lines.Length && lines[i] != "Tick: 0")
                            {
                                _logs.Add(new List<List<string>>());
                                _maps.Add(new List<List<string>>());
                                _frame_nr.Add(0);
                                while (i < lines.Length && lines[i] != "<tick>")
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) != "Tick:")
                                        break;
                                    else
                                        ++i;
                                ++i;
                                while (i < lines.Length && lines[i] != "<log>")
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                    {
                                        --i;
                                        break;
                                    }
                                    else
                                        ++i;
                                ++i;
                                _logs[_map_number].Add(new List<string>());
                                while (i < lines.Length && lines[i] != "</log>")
                                {
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                        break;
                                    else
                                    {
                                        _logs[_map_number][_frame_nr[_map_number]].Add(lines[i]);
                                        ++i;
                                    }
                                }
                                while (i < lines.Length && lines[i] != "<map>")
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                    {
                                        --i;
                                        break;
                                    }
                                    else
                                        ++i;
                                ++i;
                                _maps[_map_number].Add(new List<string>());
                                while (i < lines.Length && lines[i] != "</map>")
                                {
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                        break;
                                    else
                                    {
                                        _maps[_map_number][_frame_nr[_map_number]].Add(lines[i]);
                                        ++i;
                                    }
                                }
                                while (i < lines.Length && lines[i] != "<log>")
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                    {
                                        --i;
                                        break;
                                    }
                                    else
                                        ++i;
                                ++i;
                                while (i < lines.Length && lines[i] != "</log>")
                                {
                                    if (lines[i].Length>=5 && lines[i].Substring(0, 5) == "Tick:")
                                        break;
                                    else
                                    {
                                        _logs[_map_number][_frame_nr[_map_number]].Add(lines[i]);
                                        ++i;
                                    }
                                }
                                _frame_nr[_map_number]++;

                                while (i < lines.Length && lines[i] != "</tick>")
                                    if (lines[i].Length >= 5 && lines[i].Substring(0, 5) == "Tick:")
                                    {
                                        --i;
                                        break;
                                    }
                                    else
                                        ++i;
                                while (i < lines.Length && (lines[i].Length < 5 || lines[i].Substring(0, 5) != "Tick:"))
                                    ++i;
                            }
                            _map_number++;
                        }
                        else
                        {
                            i++;
                        }
                    }
                    numericUpDown1.Enabled = true;
                    numericUpDown2.Enabled = true;
                    checkBox1.Enabled = true;
                    numericUpDown1.Maximum = _frame_nr[0]-1;
                    numericUpDown2.Maximum = _map_number;
                    
                    update();

                    numericUpDown1.Focus();
                }
                catch (IOException)
                {
                    MessageBox.Show("Játék betöltése sikertelen!" + Environment.NewLine + "Hibás az elérési út, vagy a fájlformátum.", "Hiba!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            update();
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            numericUpDown1.Maximum = _frame_nr[Convert.ToInt32(numericUpDown2.Value) - 1] - 1;
            numericUpDown1.Value = 0;
            update();
        }

        private void update()
        {
            listBox1.Items.Clear();
            Int32 frame = Convert.ToInt32(numericUpDown1.Value);
            Int32 map = Convert.ToInt32(numericUpDown2.Value) - 1;
            for (Int32 i = 0; i < _maps[map][frame].Count; ++i)
                listBox1.Items.Add(_maps[map][frame][i]);
            update2();
        }

        private void update2()
        {
            Int32 frame = Convert.ToInt32(numericUpDown1.Value);
            Int32 map = Convert.ToInt32(numericUpDown2.Value) - 1;
            listBox2.Items.Clear();
            listBox2.Items.Add("Frames: [" + numericUpDown1.Minimum.ToString() + ".." + numericUpDown1.Maximum.ToString() + "]");
            if (checkBox1.Checked)
                for (Int32 i = 0; i < _logs[map][frame].Count; ++i)
                    listBox2.Items.Add(_logs[map][frame][i]);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            update2();
        }
    }
}
